import { defineConfig } from 'vite';
import Icons from 'unplugin-icons/vite';
import { FileSystemIconLoader } from 'unplugin-icons/loaders';
import vue from '@vitejs/plugin-vue';
import pxtovw from 'postcss-px-to-viewport-8-plugin';
import requireTransform from 'vite-plugin-require-transform';
import path from 'path';

const devhost = 'https://shenghuocang.ieieye.com'; //延吉物业：     shc shc
// const devhost = 'https://spc.ieieye.com';          //银澜：     yinlan yinlan
// const devhost = 'https://mingao.ieieye.com';       //同健远大： tjyd  tjyd
// const devhost = 'https://ledo.ieieye.com';         //来多：     ledo ledo
// const devhost = 'https://haimu.ieieye.com/';       //海姆公司： haimu test123123
// const devhost = 'https://njnw.ieieye.com/';        //南京宁卫： njnw 123456
// const devhost = 'https://demo2.ieieye.com/';       //环济云：   demo2 demo2
// const devhost = 'https://demo.ieieye.com/';        //环济云：   hjy123-test123123
                                                      //王峰：     13366668899-668899
                                                      //水流域：   18210381668
                                                      //同研：     13916366554
// const devhost = 'https://tghilyte.ieieye.com/';    //高美特例： 15537037137/zjzjzj333
// const devhost = 'https://bengfang.ieieye.com/';    //主题2展示：qingpu test123456 15600003456

// const devhost = 'http://wushuichang.ieieye.com';   //测试环境： test2/test123123
// const devhost = 'https://shhjbh.ieieye.com';       //无


/* 打包配置 */
const specialName = 'toolsSpecialName-';
const Tools = [
  'js-cookie', 'axios',
  'echarts', 'element-plus', 'file-saver', 'js-base64',
  'pinia', 'ts-md5', 'unplugin-icons',
  'vue-draggable-plus', 'vue-echarts', 'vue-router',
  'exceljs', 'dayjs', /* exceljs使用了dayjs */
];

export default defineConfig({
  plugins: [
    vue(),
    requireTransform({
      fileRegex: /.ts$|.vue$/,
    }), //   使用require形式引入图片
    Icons({
      compiler: 'vue3',
      autoInstall: true,
      customCollections: {
        custom: FileSystemIconLoader('src/topic02/assets/svg'),//使用方式：'~icons/custom/【svg图片名称，不要后缀】'
      }
    }),
    
  ],
  resolve: {
    alias: {
      '@C': path.resolve(__dirname, './src/common'),
      '@T2': path.resolve(__dirname, './src/topic02'),
      vue: 'vue/dist/vue.esm-bundler.js',/* stepEvent抽屉的问题 */
    },
  },
  server: {
    open: true,
    cors: true,
    host: '0.0.0.0',
    port: 5173,
    proxy: {
      '/developHost': {
        target: devhost,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/developHost/, ''),
        headers: {
          Referer: "https://shenghuocang.ieieye.com"
        }
      },
      '/bimfaceHost': {
        target: 'https://api.bimface.com/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/bimfaceHost/, ''),
      },
    },
  },
  base: '/',
  build: {
    chunkSizeWarningLimit: 2048,
    reportCompressedSize: false,
    rollupOptions: {
      output: {
        entryFileNames: `entry/[name]-[hash].js`,
        // assetFileNames: `asset/[ext]/[name]-[hash].[ext]`,
        assetFileNames: ({ name }) => {
          if (name.includes(specialName)) {
            const tool = Tools.find(item => name.includes(item));
            return `tools/assets/${ tool }-[hash].[ext]`;
          } else {
            return `asset/[ext]/${ name }-[hash].[ext]`;
          }
        },
        // chunkFileNames: `chunk/[name]-[hash].js`,
        chunkFileNames: ({ name }) => {
          if (name.includes(specialName)) {
            const tool = Tools.find(item => name.includes(item));
            return `tools/js/${ tool }-[hash].js`;
          } else {
            return `chunk/${ name }-[hash].js`;
          }
        },
        manualChunks: (id: string, {}) => {
          if (id.includes('node_modules')) {
            const name = Tools.find(item => id.includes(item));
            if (name) {
              return `${ specialName }${ name }`;
            }
          }
        },
      },
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
      }
    },
    postcss: {
      plugins: [
        pxtovw({
          viewportWidth: 1920,
          unitPrecision: 6,
          /* px-to-viewport-ignore */
          exclude: [/^(?!(.*pxtovw|.*pumpSys_vw))/],
        })
      ],
    },
  }
});
